<template>
    <div class = "changepassword">
        <el-container >
            <el-container>
            <el-main v-loading="loading">
                <div class = "Box-wrapper">
                <div class = "Box">
                    <el-col>
                    <el-card shadow="hover">
                    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                    <div class = "title-wrapper">
                        <p>修改密码</p>
                    </div>
                    <el-form-item label="旧密码" prop="oldpassword">
                        <el-input type="password" v-model="ruleForm.oldpassword" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="新密码" prop="newpassword">
                        <el-input type="password" v-model="ruleForm.newpassword" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="确认密码" prop="newpasswordone">
                        <el-input type="password" v-model="ruleForm.newpasswordone" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item style="float:right">
                        <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
                        <el-button @click="resetForm('ruleForm')">重置</el-button>
                    </el-form-item>
                    </el-form>
                    </el-card>
                </el-col>
                </div>
                </div>
            </el-main>
            </el-container>
        </el-container>
    </div>
</template>
<script>
export default {
    data(){
      var validateoldpassword = (rule, value, callback, Boole) => {
          console.log(value)
        if (!value) {
          return callback(new Error('密码不能为空'));
        } else {
            callback();
        }
      };
      var newpassword = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('密码不能为空'));
        } else {
                callback();
        }
      };
      var newpasswordone = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('密码不能为空'));
        } else {
            if(this.ruleForm.newpassword === this.ruleForm.newpasswordone){
                callback();
            } else {
                return callback(new Error('输入密码不一致'));
            }
        }
      };
        return{
        ruleForm: {
          oldpassword: '',
          newpassword: '',
          newpasswordone:''
        },
        loading: false,
        rules: {
          oldpassword: [
            { validator: validateoldpassword, trigger: 'blur' }
          ],
          newpassword: [
            { validator: newpassword, trigger: 'blur' }
          ],
          newpasswordone: [
            { validator: newpasswordone, trigger: 'blur' }
          ],
        },
        api:''
        }
    },
    created() {
        this.apione()
    },
    methods:{
    apione(){
            this.$api.publickey.key().then(res => {
                this.api = res.data
            }).catch(err => {
                this.$message.error(err); //登录失败提示错误
            });
        },
    open1(title,content,type) {
        this.$notify({
          title: title,
          message: content,
          type: type
        });
    },
    submitForm(formName) {
         this.$refs[formName].validate((valid) => {
          if (valid) {
            let data = this.$getCode(this.ruleForm.oldpassword,this.api)
            let dataone = this.$getCode(this.ruleForm.newpasswordone,this.api)
            let form = JSON.parse(sessionStorage.getItem('user'))
            let password = {
              oldPwd:data,
              newPwd:dataone
            }
            this.$api.personalcenter.changepassword(password,form.id).then(res => {
              if(res.code === 2110){
                this.open1('失败','密码错误','warning')
              } else if(res.code === 200){
                this.open1('成功','修改成功','success')
                this.$refs[formName].resetFields();
              }
            }).catch(err => {
              console.log(err)
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
       resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
}
</script>
<style scoped>
    .changepassword{
        width:100%;
    }
    .el-main{
        width:100%;
    }
    .changepassword .Box-wrapper{
        width:100%;
        margin-top:12%;
        display: flex;
        justify-content:center;
    }
    .changepassword .Box-wrapper .Box {
        width:500px;       
    }
    .changepassword .Box-wrapper .Box .title-wrapper{
        padding:20px 0;
        font-size:25px;
        text-align: center;
    }
</style>
